package rabbitmq.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeoutException;
import rabbitmq.common.Const;

/**
 * @author zss
 * @date 2021/8/27 17:32
 * @Email:451682479@qq.com
 */
public class TopicProvider {

    private final static String VIRTUAL_HOST = "devHost";

    private static final String EXCHANGE_NAME = "topic_exc";

    public static void main(String[] args) {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Const.host);
        factory.setUsername(Const.username);
        factory.setPassword(Const.password);
        factory.setVirtualHost(VIRTUAL_HOST);

        try {
            Connection connection = factory.newConnection();

            Channel channel = connection.createChannel();

            channel.exchangeDeclare(EXCHANGE_NAME, "topic");

            String message = "消息消息 ";

            for (int i = 0; i < 10000; i++) {

                String routingKey = "";
                if (i % 3 == 0) {
                    routingKey = "abc";
                } else if (i % 2 == 0) {
                    routingKey = "abc.def";
                } else {
                    routingKey = "abc.def.ghi";
                }

                message = routingKey + " ==> @@" + i + "@@";

                channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());

                System.out.println(String.format("生产者: msg = %s, routingKey = %s ", message, routingKey));
                Thread.sleep(500);
            }

        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
